package com.demo.redis;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 布隆过滤器
 * */
@RestController
@RequestMapping("/bloom")
public class BloomFilterDemo {

    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("/test")
    public void bloomTest(){
        //获取一个布隆过滤器
        RBloomFilter<Object> mbf = redissonClient.getBloomFilter("myBloomFilter");
        //尝试初始化，预计元素 100，允许误判率 0.05
        mbf.tryInit(100L, 0.05);

        for(int i = 0;i < 1000;i++){
            System.out.println(i + "：" + mbf.add(i));
        }
        System.out.println(mbf.getSize());
    }
}
